/**
 * Filename: PopSetAvatar.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/18 15:08
 */
package com.github.postsanf.yinian.activity.pop;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.utils.CashierInputFilter;

public class PopTopayZidingyi extends PopupWindow {
    public View mMenuView;
    private InputMethodManager imm;
    private RelativeLayout rl_tixian1;
    private RelativeLayout rl_tixian2;
    private TextView to_pay_cash;
    private EditText getcash_num;
    public String price;

    public PopTopayZidingyi(final Activity context, OnClickListener onClickListener) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        mMenuView = inflater.inflate(R.layout.pop_to_pay_zidingyi, null);
        this.setWidth(LayoutParams.MATCH_PARENT);
        this.setHeight(LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setAnimationStyle(R.style.AnimBottom);
        setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setBackgroundDrawable(new ColorDrawable(0x99000000));
        this.setContentView(mMenuView);
        mMenuView.findViewById(R.id.mywallet_leftback).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mMenuView.findViewById(R.id.to_pay_close).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                context.sendBroadcast(new Intent(ACTIONs.actionCloseactivity));
                dismiss();
            }
        });
        rl_tixian1 = (RelativeLayout) mMenuView.findViewById(R.id.rl_tixian1);
        rl_tixian2 = (RelativeLayout) mMenuView.findViewById(R.id.rl_tixian2);
        to_pay_cash = (TextView) mMenuView.findViewById(R.id.to_pay_cash);
        getcash_num = (EditText) mMenuView.findViewById(R.id.getcash_num);
        getcash_num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getcash_num.setSelection(getcash_num.getText().length());
            }
        });
        InputFilter[] inputFilters = {new CashierInputFilter()};
        getcash_num.setFilters(inputFilters);
        getcash_num.setFocusable(true);
        getcash_num.setFocusableInTouchMode(true);
        getcash_num.requestFocus();
        getcash_num.setSelection(getcash_num.getText().length());
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        rl_tixian1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                rl_tixian2.setVisibility(View.VISIBLE);
                rl_tixian1.setVisibility(View.GONE);
                getcash_num.setFocusable(true);
                getcash_num.setFocusableInTouchMode(true);
                getcash_num.requestFocus();
                getcash_num.setSelection(getcash_num.getText().length());
            }
        });
        mMenuView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.pop_setuser_advatar_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
        getcash_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                price = s.toString();
                if (s.length() > 0) {
                    rl_tixian2.setVisibility(View.VISIBLE);
                    rl_tixian1.setVisibility(View.GONE);
                    to_pay_cash.setBackgroundResource(R.drawable.btn_topay_sure_press);
                    if (Double.parseDouble(s.toString()) > 200) {
                        Toast.makeText(context, "充值金额两百以内", Toast.LENGTH_SHORT).show();
                        getcash_num.setText("200");
                        getcash_num.setSelection(getcash_num.getText().length());
                        return;
                    }
                } else {
                    to_pay_cash.setBackgroundResource(R.drawable.btn_topay_sure);
                }
            }
        });
        to_pay_cash.setOnClickListener(onClickListener);
    }

}
